package com.ktjiaoyu.web;

import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.utils.Constants;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;

import java.lang.module.Configuration;

public class AuthInterceptor implements HandlerInterceptor {
    private static final Logger logger= LoggerFactory.getLogger(AuthInterceptor.class);

    //前置处理方法,进入控制器之前经过此方法判断是否能够继续请求
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.debug("进入自定义拦截器AuthInterceptor,请求路劲:{}", request.getRequestURI());
        HttpSession session=request.getSession();
        //获取用户登录信息
        SysUser sysUser=(SysUser) session.getAttribute(Constants.SESSION_USER);
        //检查是否登录
        if (sysUser==null){
            //没有登录信息,跳转到401.jsp页面
            request.getRequestDispatcher("/WEB-INF/jsp/401.jsp").forward(request,response);
            return false;
        }
        return true;
    }
}